<?php

namespace addons\booking\model;

use think\Model;

class UserBank extends Model
{
    // 表名
    protected $name = 'user_bank';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $deleteTime = false;

    protected  $str='7308403a01eb7ccc44acf0d1037a5c47';

    protected  $appid=28378;
    //查询银行卡信息
    public  function getBankInfo($BankCode)
    {
        $method = 'GET'; //请求方式 GET,POST
        $secretType = 'MD5'; //验证方式MD5,Hash 通过后台 我的应用去修改
        $api_url = 'https://uaqy.api.storeapi.net/pyi/84/202';
        $appid  =   $this->appid;// 在后台我的应用查看;
        $secret =   $this->str;// 在后台我的应用查看;
        $data = array(
            'appid'=>  $appid,
            'bank_card'=> $BankCode,
            'format'=>  'json',
            'time'=>  time(),
        );
        if('MD5' == $secretType){
            ksort($data); //按照键名对数组排序，为数组值保留原来的键。
            $md5String = '';
            foreach($data as $key=>$val){
                if(strlen($val)>0){ //过滤空值
                    $md5String.=$key.$val;
                }
            }
            $secret = md5($md5String.$secret);
        }
        $data['sign'] = $secret;
        if('GET'==$method){
            $sendUrl = $api_url.'?'.http_build_query($data); //把数据转换成url参数形式，a=b&c=d&e=f
            $result = file_get_contents($sendUrl);
        }else{
            $this->curl_bank($data,$api_url);
        }
        return $result = json_decode($result,true);
    }
    //查找银行卡列表
    public static function getUserBankList($userId)
    {
        return self::where('user_id',$userId)->select();
    }
    public function curl_bank($data,$api_url)
    {
        $header = ['Content-Type: application/x-www-form-urlencoded;charset=utf-8'];
        $ch = curl_init();
        if(is_array($data))$data = http_build_query($data);
        curl_setopt($ch, CURLOPT_URL, $api_url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, true);//POST
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_ENCODING,'gzip,deflate');
        $result = curl_exec($ch);
        return $result;
    }

}